//
//  SearchResultTableViewController.swift
//  Reid
//
//  Created by 萧瑶 on 2022/5/3.
//

import UIKit

class SearchResultTableViewController: UITableViewController,UISearchResultsUpdating {
    
    var allInfo: [Dictionary<String, [String : Any]>.Element]?
    var filterInfo: [Dictionary<String, [String : Any]>.Element] = []
    var filterPlace: [String]?
    
    func updateSearchResults(for searchController: UISearchController) {
        guard let searchString = searchController.searchBar.text else { return }
        if searchString.isEmpty { return }
        switch searchController.searchBar.selectedScopeButtonIndex
        {
        case 0:
            filterInfo = allInfo!.filter{
                (searchInfo) -> Bool in return
                searchInfo.key.contains(searchString)}
        default:
            return
        }
        
        tableView.reloadData()
    }
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let xib = UINib(nibName: "UItableTableViewCell", bundle: Bundle.main)
        tableView.register(xib, forCellReuseIdentifier: "cell")
        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return filterInfo.count
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! UItableTableViewCell
        
        let numofpeople = filterInfo[indexPath.row].value["present"] as! Double
        let numofpeople10ago = filterInfo[indexPath.row].value["10ago"] as! Double
        let timeperson = abs(numofpeople - numofpeople10ago) / 10
        cell.timeperperason.text = String(format: "%1.1f", timeperson)+"分钟"
        cell.numofpeople.text = "\(numofpeople)人/分钟"
        cell.placeName.text = filterInfo[indexPath.row].key
        cell.mainImage.image = UIImage(named: filterInfo[indexPath.row].key)
        
         return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let mainBoard: UIStoryboard! = UIStoryboard(name: "Main", bundle: nil)
        let detail = mainBoard.instantiateViewController(withIdentifier: "detail") as! detailViewController
        
        detail.count = filterInfo[indexPath.row].value["present"] as? Int
        detail.flow = filterInfo[indexPath.row].value["flow"] as? Double
        detail.time = filterInfo[indexPath.row].value["time"] as? Double
        detail.dist = filterInfo[indexPath.row].value["dist"] as? Double
        detail.englishName = filterInfo[indexPath.row].value["englishName"] as? String
        
        present(detail, animated: true, completion: nil)
    }

    /*
    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // Delete the row from the data source
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
